package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.BeanMapUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController{

    @Reference
    private ShippingService shippingService;

    @Reference
    private PackingService packingService;

    @Reference
    private ExportService exportService;


    //进行分页查询
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        ShippingExample example = new ShippingExample();

        // 根据create_time 创建时间，倒序排序
        example.setOrderByClause("create_time desc");

        ShippingExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo<Shipping> pageInfo = shippingService.findByPage(example, pageNum, pageSize);

        request.setAttribute("pageInfo",pageInfo);
        return "/cargo/shipping/shipping-list";
    }


    //跳转添加页面   查询装箱列表
    @RequestMapping("/toAdd")
    public String toAdd(String id){
        Packing packing = packingService.findById(id);
        Export export = exportService.findById(packing.getExportIds());
        Shipping shipping = new Shipping();

        // 设置委托单id
        shipping.setId(packing.getPackingListId());
        // 设置信用证
        shipping.setLcNo(export.getLcno());
        // 设置装箱港
        shipping.setPortOfLoading(export.getShipmentPort());
        // 设置卸货港
        shipping.setPortOfDischarge(export.getDestinationPort());
        // 设置委托单状态
        shipping.setState(0);

        String marks = packing.getMarks();
        request.setAttribute("marks",marks);
        request.setAttribute("shipping",shipping);
        return "cargo/shipping/shipping-add";
    }


    //保存
    @RequestMapping("/edit")
    public String edit(Shipping shipping){
        shipping.setCompanyId(getLoginCompanyId());
        shipping.setCompanyName(getLoginCompanyName());

        shippingService.save(shipping);

        return "redirect:/cargo/shipping/list.do";
    }



    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        shippingService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping("/state")
    @ResponseBody
    public Map<String,Integer> state(String id) {
        Map<String,Integer> result = new HashMap<>();
        Shipping shipping = shippingService.findById(id);
        if (shipping.getState() == 0){
            // 该委托单状态为草稿,可以删除
            result.put("message",1);
        }else {
            // 该委托单为已生成发票,不可删除
            result.put("message",0);
        }
        return result;
    }

    @Autowired
    private DataSource dataSource;

    @RequestMapping("/shippingPdf")
    public void exportPdf(String id) throws Exception {
        //1. 加载jasper文件，获取文件流
        InputStream in =
                httpSession.getServletContext().getResourceAsStream("/jasper/shipping.jasper");

        //2. 创建JasperPrint对象
        Connection connection = dataSource.getConnection();

        //参数1:模板文件输入流；参数2：传递到模板文件中的key-value类型的参数；参数3：数据列表参数
        JasperPrint jasperPrint =
                JasperFillManager.fillReport(in,new HashMap<>(),connection);
        //3. 以pdf形式输出
        /*JasperExportManager.
                exportReportToPdfStream(jasperPrint,response.getOutputStream());*/
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-disposition","attachment;fileName=export.pdf");
        ServletOutputStream outputStream = response.getOutputStream();
        JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
        outputStream.close();


    }

}
